<template>
  <div class="header">
    <div class="header-content">
      <div class="header-left">
        <div class="header-left-ul">
          <div class="header-left-logo">
            <img src="../../assets/images/logo.jpg">
          </div>
          <!-- <li @click="$router.push('/')" class="on">首页</li> -->
          <li @click="getHref" class="on" :class="{select:(href=='')}">
            <router-link to="/" class="link">首页</router-link>
          </li>
          <li class="classification on">分类浏览
            <div class="box">
              <li
                v-for="(item,index)  in allList"
                :key="index"
                @click="listlink(item)"
              >{{item.title}}</li>
            </div>
          </li>
          <!-- <li @click="$router.push('/university')" class="on">免费课</li>
          <li @click="$router.push('/competitor')" class="on">精品课</li>
          <li @click="$router.push('/consultation')" class="on">企业咨询</li>-->
          <li @click="getHref" class="on" :class="{select:(href=='university')}">
            <router-link to="/university" class="link">免费课</router-link>
          </li>
          <li @click="getHref" class="on" :class="{select:(href=='competitor')}">
            <router-link to="/competitor" class="link">精品课</router-link>
          </li>
          <li @click="getHref" class="on" :class="{select:(href=='consultation')}">
            <router-link to="/consultation" class="link">企业咨询</router-link>
          </li>
        </div>
      </div>
      <div class="header-center">
        <input type="text" placeholder="搜索您感兴趣的知识" v-model="searchall" @keyup.enter="search">
        <i class="iconfont icon-all-sousuo1" style="cursor: pointer;" @click="search" ></i>
        <span @click="getQuestion">我问</span>
      </div>
      <div class="header-right">
        <!-- <li  @click="goLogin" class="on">讨教号</li> -->
        <li @click="getHref" class="on" :class="{select:(href=='number')}">
          <router-link to="/number" class="link">讨教号</router-link>
        </li>
        <li class="app on">
          下载APP
          <img src="../../assets/images/rwm.png" alt>
        </li>
        <li class="login on" @click="goLogin" v-if="noLoginShow">登录 | 注册</li>
        <li class="longin-box" v-if="loginShow">
          <!-- <i class="iconfont icon-all-tongzhi"></i> -->
          <img :src="this.img" alt >
          <!-- 个人中心 -->
          <div class="personal">
            <p class="name">{{this.user}}</p>
            <li @click="$router.push('/wz')" v-if="this.author == 2">我的文章</li>
            <li @click="$router.push('/sc')">我的收藏</li>
            <li @click="$router.push('/wd')">我的问答</li>
            <li @click="$router.push('/pl')">我的评论</li>
            <li @click="$router.push('/dy')">我的订阅</li>
            <li @click="$router.push('/ds')">我的打赏</li>
            <!-- <li>资料修改</li> -->
            <p class="goout" @click="goOut">退出登录</p>
          </div>
        </li>
      </div>
    </div>
    <!-- 我问 -->
    <div class="question" v-if="show">
      <div class="model">
        <div class="model-ask">
          <div class="header1">
            <h3>
              提问
              <span class="close" @click="close">×</span>
            </h3>
          </div>
          <div class="main">
            <div class="main-title">
              <img :src="this.img" alt>
              <span>写下你的问题，准确的表述更容易得到答案</span>
            </div>
            <div class="main-content">
              <input type="text" placeholder="请输入问题" v-model="question">
            </div>
            <p>类型</p>
            <select class="main-select" v-model="type"  @change="changeType($event)" >
                  <!-- <option selected = "selected" >全部</option>  -->
                  <option :value='item.id' v-for="(item,index) in questionList" :key="index"   >
                    {{item.title}}
                  </option>
            </select>
            <div class="main-ms">
              <div class="ms-des">
                <textarea placeholder="必填，详细说明您的问题，以便获得更好的答案" v-model="describe"></textarea>
              </div>
              <!-- <div class="ms-plus">

              </div>-->
            </div>
            <div class="main-foot">
              <div class="main-foot-l">
                <!-- <span>匿名提问</span>
                <span>提问悬赏</span> -->
              </div>
              <div class="main-foot-r" @click="sumbit">发布问答</div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <!-- 登录注册 -->
    <div class="login1" v-if="flag">
      <div class="model">
        <div class="model-ask">
          <div class="header2">
            <div class="close" @click="goClose">×</div>
            <h4>登录</h4>
            <p>讨教 | 通行证</p>
          </div>
          <div class="content1">
            <div class="photo">
              <span>
                <i class="iconfont icon-all-shouji"></i>
              </span>
              <input type="text" placeholder="手机号" v-model="number">
            </div>
            <div class="password">
              <span>
                <i class="iconfont icon-all-mima"></i>
              </span>
              <input type="password" placeholder="登录密码" v-model="password">
            </div>
            <p v-if="text">
              <i class="iconfont icon-all-jinggao-"></i>
              <em>{{defaultCode}}</em>
            </p>
            <!-- <p v-if="psw">
              <i class="iconfont icon-all-jinggao-"></i>
              <em>请输入密码</em>
            </p>
            <p v-if="error">
              <i class="iconfont icon-all-jinggao-"></i>
              <em>登录名或密码错误，请重试</em>
            </p>-->
            <div class="btn" @click="goEntry">登录</div>
            <div class="msg">
              <div @click="$router.push('/register')">立即注册</div>
              <div @click="$router.push('/password')">忘记密码?</div>
            </div>
          </div>
          <div class="footer1">
            <i class="iconfont icon-all-weixin"></i>
            使用微信登录
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import eventBus from '@/common/js/eventBus.js';
import $ from "jquery";
import qs from "qs";
export default {
  name: "Header",
  data() {
    return {
      personal: {},
      defaultCode: "请输入有效的手机号码",
      text: false,
      noLoginShow: true,
      loginShow: false,
      password: "",
      number: "",
      show: false,
      flag: false,
      username: false,
      psw: false,
      error: false,
      allList: [],
      sizeIndex: 10,
      href: "",
      infoId: "",
      questionList:[],
      question:'',
      describe:'',
      id:0,
      type:0,
      searchall:'',
      searchList:[],
      searchIndex:5
    };
  },
  methods: {
    getHref() {
      let hrefArr = window.location.href.split("/");
      this.href = hrefArr[hrefArr.length - 1];
      // console.log(this.href)
    },
    //获取分类文章的数据
    listlink(item) {
      this.$router.push({
        path: "/classification",
        query: { infoId: item.id }
      });
    },
    getQuestion() {
      this.show = true;
    },
    close() {
      this.show = false;
    },
    goLogin() {
      this.flag = true;
    },
    goClose() {
      this.flag = false;
    },
    //登录
    goEntry() {
      console.log(this.number);
      if (!this.phoneReg.test(this.number)) {
        this.text = true;
        return;
      }
      if (this.password === "") {
        this.defaultCode = "密码不能为空";
        this.text = true;
        return;
      }
      let param = {
        login_password: this.password,
        mobile: this.number
      };
      this.$axios
        .post("api/App/User/Register/login", qs.stringify(param), {
          headers: { "Content-Type": "application/x-www-form-urlencoded" }
        })
        .then(res => {
          if (res.data.status == 1) {
            this.flag = false;
            this.personal = res.data.data;
            this.noLoginShow = false;
            this.loginShow = true;
            this.setCookie(
              "token",
              encodeURIComponent(res.data.data.rong_token, "utf-8")
            );
            this.setCookie(
              "userName",
              encodeURIComponent(res.data.data.nickname, "utf-8")
            );
            this.setCookie(
              "nm_avatar",
              encodeURIComponent(res.data.data.nm_avatar, "utf-8")
            );
            this.setCookie(
              "member_id",
              encodeURIComponent(res.data.data.member_id, "utf-8")
            );
            this.setCookie(
              "identity_type",
              encodeURIComponent(res.data.data.identity_type, "utf-8")
            );
            // this.$router.push("/");
            this.user = decodeURI(escape(this.getCookie("userName")));
            this.img = this.getCookie("nm_avatar")
          } else if (res.data.status === "0") {
            this.defaultCode = "用户不存在或密码错误";
            this.text = true;
          } else {
            this.defaultCode = "请求错误";
            this.text = true;
          }
        });
    },
    goOut() {
      this.$router.push("/");
      this.noLoginShow = true;
      this.loginShow = false;
    },
    //获取文章分类列表
    getList() {
      this.$axios.get("api/App/User/News/get_category_list", {}).then(res => {
        this.allList = res.data.data.news_category;
        // console.log(this.allList);
      });
    },
    // 获取问答类型
    getQuestionList(){
       this.$axios.get("api/App/User/Question/category_list", {}).then(res => {
         console.log(res)
        this.questionList = res.data.data.questions_category;
        // console.log(this.allList);
      });
    },
    //发布问题
    sumbit(){
       //问题和描述非空验证
       if(this.question === ''){
          this.$confirm('问题不能为空', '提示', {
          type: 'warning'
        })
        return;
       }
       if(this.describe.length<5){
          this.$confirm('问题描述不能小于5个字符', '提示', {
          type: 'warning'
        })
        return;
       }
      //  console.log(this.id)
       let param = {
        title: this.question,
        member_id:this.getCookie("member_id"),
        category_id:this.id,
        description:this.describe,
        queestions_type:1,
      }; 
      this.$axios
        .post("/api/App/User/Question/publish", qs.stringify(param), {
          headers: { "Content-Type": "application/x-www-form-urlencoded" }
        }).then(res=>{
         this.$alert('操作成功', '提示', {
          confirmButtonText: '确定',
          
        });
        this.show =false;
        })

    },
    //获取类型ID
    changeType(event){
        this.id =event.target.value;
    },
    //搜索关键字
    search(){
      if(this.searchall=== ''){
         alert('搜索关键字不能为空')
        return;
      }
      let param = {
        keywords: this.searchall,
        limit:this.searchIndex
        
      }; 
      this.$axios
        .post("/api/App/User/Question/all_search_result", qs.stringify(param), {
          headers: { "Content-Type": "application/x-www-form-urlencoded" }
        }).then(res=>{
          console.log(res)
          this.searchList = res.data.data
          console.log(this.searchList)  
          // this.$router.push("/searchAll");
          setTimeout(() => {
            eventBus.$emit('header',this.searchList)
            eventBus.$emit('headerlist',this.searchall)
          },0)
          this.$router.push({path:"/searchAll"});  
          // eventBus.$emit('header',this.searchList)      
        })
    }
  },
  mounted() {
    // this.questionReg =/^[A-Za-z0-9\u4e00-\u9fa5]+$/
    this.phoneReg = /^1[34578]\d{9}$/;
    if (
      this.getCookie("token")&&
      this.getCookie("userName")
    ) {
      this.noLoginShow=false;
      this.loginShow = true;
      this.user = decodeURI(escape(this.getCookie("userName")));
      this.img = this.getCookie("nm_avatar");
      this.author =this.getCookie("identity_type") 
    }
  },
  created() {
    this.getHref();
    // console.info(this.getCookie("token") + "_" + this.getCookie("userName"));
    this.getList();
    this.getQuestionList();
  },
  beforeDestroy(){
      // this.$root.eventBus.$emit('header',this.searchList)
      eventBus.$emit('header',this.searchList) 
  },
};
</script>
<style lang="scss" scoped>
.header {
  position: fixed;
  top: 0;
  width: 100%;
  height: 70px;
  background: #aed6ff;
  z-index: 999;
  .classification:hover {
    .box {
      visibility: visible !important;
    }
  }
  .box {
    position: absolute;
    top: 70px;
    left: 12%;
    background: #fff;
    width: 260px;
    border-radius: 10px;
    visibility: hidden;
    display: flex;
    flex-wrap: wrap;
    padding: 10px 0;
    li {
      cursor: pointer;
      width: 33% !important;
      font-size: 10px !important;
      height: 30px !important;
      line-height: 30px !important;
      // color: #e3e3e3 !important;
      border-right: 1px dashed #e3e3e3 !important;
      &:hover {
        border-bottom: 1px solid #5b90bc !important;
      }
    }
  }
  .header-content {
    display: flex;
    width: 90%;
    // height: 50px;
    margin: 0 auto;
    .active {
      color: #5b90bc;
      border-bottom: 3px solid #5b90bc;
    }
    .header-left {
      width: 40%;
      .header-left-logo {
        height: 70px;
        width: 70px;
        img {
          display: block;
          width: 80%;
          height: 80%;
          margin-top: 10%;
        }
      }
      .header-left-ul {
        height: 70px;
        display: flex;
        justify-content: space-between;
        align-items: center;
        li {
          list-style: none;
          font-size: 18px;
          color: #333;
          height: 70px;
          line-height: 70px;
          &:hover {
            // color: #5b90bc;
            border-bottom: 3px solid #5b90bc;
          }
        }
      }
    }
    .header-center {
      flex: 1;
      width: 90%;
      margin: 0 auto;
      input {
        height: 30px;
        width: 65%;
        margin-top: 20px;
        box-sizing: border-box;
        border-radius: 10px;
        outline: none;
        border: 0px;
      }
      i {
        margin-left: -30px;
        margin-right: 20px;
      }
      input::-webkit-input-placeholder {
        color: #666;
        font-size: 12px;
        padding-left: 10px;
        letter-spacing: 2px;
      }
      input:-moz-placeholder {
        color: #666;
        font-size: 12px;
        padding-left: 10px;
        letter-spacing: 2px;
      }
      input:-ms-input-placeholder {
        color: #666;
        font-size: 12px;
        padding-left: 10px;
        letter-spacing: 2px;
      }

      span {
        display: inline-block;
        height: 30px;
        width: 60px;
        line-height: 30px;
        margin-left: 10px;
        border-radius: 10px;
        color: #fff;
        background-color: #5d90be;
        // outline: none;
        border: 0px;
        cursor: pointer;
      }
    }
    .header-right {
      width: 25%;
      height: 70px;
      display: flex;
      justify-content: space-between;
      align-items: center;
      li {
        list-style: none;
        font-size: 20px;
        color: #000;
        height: 70px;
        line-height: 70px;
        &:hover {
          color: #5b90bc;
          border-bottom: 3px solid #5b90bc;
        }
      }
      .longin-box {
        width: 110px;
        // margin-right: 90px;
        //     margin-left: 90px;
        // padding-right: 6px;
        &:hover {
          border-bottom: 3px solid red !important;
          .personal {
            display: block;
          }
        }
        .personal {
          width: 100px;
          background: #fff;
          border-radius: 10px;
          position: absolute;
          top: 70px;
          right: 80px;
          display: none;
          li {
            width: 100%;
            list-style: none;
            margin-top: 3px;
            height: 30px;
            line-height: 30px;
            font-size: 16px;
            cursor: pointer;
            // &:hover {
            //   color: #5b90bc;
            // }
          }
          .name {
            margin: 0;
            line-height: 1.8;
            border-bottom: 1px solid #eee;
            padding-bottom: 3px;
            margin-bottom: 10px;
          }
          .goout {
            margin-top: -10px;
          }
        }
        img {
          width: 30px;
          height: 30px;
          border-radius: 100%;
          margin-top: -7px;
        }
        i {
          font-size: 24px;
          padding-right: 10px;
        }
        &:hover {
          color: none !important;
          border-bottom: none !important;
        }
      }
      .app {
        margin-left: 30px;
        &:hover img {
          display: block;
        }
        img {
          // display: block;
          position: absolute;
          top: -30px;
          right: 190px;
          width: 150px;
          height: 150px;
          margin-top: 100px;
          display: none;
        }
      }
      .login {
        width: 110px;
        // margin-left: 30px;
        // padding-right: 6px;
      }
    }
  }
}
.classification .box li:nth-child(3n + 3) {
  border-right: 0px dashed #e3e3e3 !important;
}
.login1 {
  position: fixed;
  top: 0;
  left: 0;
  z-index: 100;
  width: 100%;
  height: 100%;
  background: #666;
  background: rgba(64, 64, 64, 0.9);

  .model {
    position: absolute;
    left: 0;
    top: 50%;
    -ms-transform: translate(0, -50%);
    -webkit-transform: translate(0, -50%);
    -moz-transform: translate(0, -50%);
    transform: translate(0, -50%);
    max-height: 100%;
    overflow: auto;
    width: 100%;
    .model-ask {
      width: 400px;
      -webkit-border-radius: 4px;
      border-radius: 4px;
      background: #fff;
      margin: 10px auto;
      .header2 {
        width: 100%;
        height: 140px;
        background: #f6f7fb;
        h4 {
          padding-top: 50px;
        }
        p {
          color: #000;
        }
        .close {
          width: 30px;
          height: 30px;
          font-size: 40px;
        }
      }
      .content1 {
        margin-top: 20px;
        width: 100%;
        background: #fff;
        height: auto;
        margin-bottom: 30px;
        .photo {
          height: 40px;
          width: 60%;
          margin: 0 auto;
          border: 1px solid #cdcfd1;
          display: flex;
          justify-content: space-between;
          border-radius: 3px;
        }
        .password {
          height: 40px;
          width: 60%;
          margin: 0 auto;
          border: 1px solid #cdcfd1;
          display: flex;
          justify-content: space-between;
          border-radius: 3px;
          margin-top: 20px;
        }
        span {
          display: block;
          width: 15%;
          border-right: 1px solid #cdcfd1;
          i {
            font-size: 25px;
          }
        }

        input {
          width: 85%;
          border: 0;
          outline: none;
          padding: 0 20px;
        }
        input::-webkit-input-placeholder {
          color: #666;
          font-size: 12px;
          padding-right: 10px;
          letter-spacing: 2px;
        }
        input:-moz-placeholder {
          color: #666;
          font-size: 12px;
          padding-right: 10px;
          letter-spacing: 2px;
        }
        input:-ms-input-placeholder {
          color: #666;
          font-size: 12px;
          padding-right: 10px;
          letter-spacing: 2px;
        }
        .btn {
          height: 40px;
          width: 60%;
          margin: 0 auto;
          background: #4786ff;
          font-size: 16px;
          color: #fff;
          margin-top: 20px;
        }
        p {
          height: 25px;
          width: 60%;
          margin: 0 auto;
          font-size: 12px;
          color: red;
          line-height: 25px;
          border: 1px solid #fc4c24;
          margin-top: 10px;
        }
        .msg {
          height: auto;
          padding-top: 10px;
          width: 60%;
          margin: 0 auto;
          display: flex;
          justify-content: space-between;
          div {
            // text-decoration: none;
            color: #666;
            font-size: 12px;
          }
        }
      }
      .footer1 {
        width: 60%;
        margin: 0 auto;
        height: 100px;
        color: #666;
        font-size: 14px;
        i {
          color: #24db5a;
          font-size: 16px;
        }
      }
    }
  }
}
.question {
  position: fixed;
  top: 0;
  left: 0;
  z-index: 101;
  width: 100%;
  height: 100%;
  background: #666;
  background: rgba(64, 64, 64, 0.9);
  // display: none;
  .model {
    position: absolute;
    left: 0;
    top: 50%;
    -ms-transform: translate(0, -50%);
    -webkit-transform: translate(0, -50%);
    -moz-transform: translate(0, -50%);
    transform: translate(0, -50%);
    max-height: 100%;
    overflow: auto;
    width: 100%;
    .model-ask {
      width: 660px;
      -webkit-border-radius: 4px;
      border-radius: 4px;
      background: #fff;
      margin: 10px auto;
      .header1 {
        width: 660px;
        height: 44px;
        border-bottom: 1px solid #f0f1f3;
        h3 {
          position: relative;
          color: #333;
          width: 660px;
          font-size: 14px;
          line-height: 44px;
          text-indent: 20px;
          .close {
            display: block;
            width: 40px;
            height: 0px;
            font-size: 65px;
            position: absolute;
            right: 10px;
            top: 0;
            color: #cacaca;
            -ms-transform: scale(0.5);
            -webkit-transform: scale(0.5);
            -moz-transform: scale(0.5);
            transform: scale(0.5);
          }
        }
      }
      .main {
        margin-top: 20px;
        width: 660px;
        .main-title {
          width: 660px;
          margin-top: 20px;
          text-align: left;
          padding: 0 50px;
          img {
            width: 60px;
            height: 60px;
            border-radius: 100%;
          }
          span {
            padding-left: 20px;
            color: #9f9f9f;
          }
        }
        .main-content {
          width: 660px;
          text-align: left;
          padding-left: 130px;
          margin-top: 10px;
          input {
            width: 400px;
            height: 40px;
            outline: none;
            // border:0;
            border: 1px solid #e8e8e8;
            border-radius: 5px;
          }
        }
        p{
          font-size: 16px;
          text-align: left;
          padding-left: 130px;
          margin-top:10px;
        }
        .main-select{
          margin-top:10px;
          width: 150px;
          height: 30px;
          display: flex;
          justify-content: flex-start;
          margin-left: 130px;
          // text-align: left;
          // margin-left:20px;
        }
        .main-ms {
          width: 660px;
          height: auto;
          padding-left: 130px;
          margin-top: 20px;
          // border:1px solid #e8e8e8;
          .ms-des {
            width: 500px;
            // margin-left:30px;
            textarea {
              width: 100%;
              height: 78px;
              color: #333;
              max-height: 520px;
              min-height: 78px;
              font-size: 14px;
              line-height: 24px;
              resize: none;
              border: 1px solid #e8e8e8;
              outline: none;
            }
          }
        }
        .main-foot {
          display: flex;
          justify-content: space-between;
          padding: 0 50px;
          margin-top: 20px;
          .main-foot-l {
            color: #9f9f9f;
            span {
              padding-right: 20px;
              font-size: 12px;
            }
          }
          .main-foot-r {
            width: 100px;
            height: 38px;
            background: #5a90ba;
            color: #fff;
            line-height: 38px;
            font-size: 14px;
            border-radius: 10px;
            margin-bottom: 20px;
            cursor: pointer;
          }
        }
      }
    }
  }
}
.select {
  .link {
    color: red !important;
    text-decoration: none !important;
    &:hover {
      text-decoration: none !important;
    }
  }
}
a {
  color: #000 !important;
  &:hover {
    text-decoration: none !important;
  }
}
</style>
